package com.hxd.otc.module.wallet.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.hxd.otc.R;
import com.hxd.otc.base.BaseAdapter;
import com.hxd.otc.module.wallet.bean.WalletCoinRateVoBean;
import com.hxd.otc.utils.UIUtils;
import com.hxd.otc.widget.CurrencyConverterRelativeLayout;

import java.math.BigDecimal;
import java.math.RoundingMode;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by lzy on 2018/7/30 0030.
 * description ${汇率换算 Adapter}
 */
public class CurrencyConverterAdapter extends BaseAdapter<RecyclerView.ViewHolder, WalletCoinRateVoBean> {
    public static final int ITEM_TYPE_NORMAL = 0;
    public static final int ITEM_TYPE_BANNER = 1;


    public CurrencyConverterAdapter(Context context) {
        super(context);
    }

    @Override
    protected void onBindHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case ITEM_TYPE_NORMAL:
                onBindNormalHolder((NormalHolder) holder, position);
                break;
            case ITEM_TYPE_BANNER:
                onBindBannerHolder((BannerHolder) holder, position);
            default:
                break;
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        switch (viewType) {
            case ITEM_TYPE_NORMAL:
                return new NormalHolder(mInflater.inflate(R.layout.item_currency_converter_view, parent, false));
            case ITEM_TYPE_BANNER:
            default:
                return new BannerHolder(mInflater.inflate(R.layout.item_currency_converter_banner, parent, false));
        }
    }

    private void onBindNormalHolder(NormalHolder holder, final int position) {
        final WalletCoinRateVoBean item = getItem(position);

        holder.rlContainer.setSelected(item.isSelected());
        holder.etInput.setText(item.getContent());
        holder.ivLeft.setImageResource(item.getResId());
        holder.tvCurrency.setText(item.getCurrencyText());
        holder.tvTitle.setText(item.getTitle());
        if (item.isSelected()) {
            mTextWatcher.addWatcher(holder.etInput);
            holder.etInput.setFocusable(true);
            holder.etInput.requestFocus();
            holder.etInput.setSelection(0, holder.etInput.getText().toString().trim().length());
            UIUtils.showSoftKeyboard(holder.etInput);
        }
    }

    private void onBindBannerHolder(BannerHolder holder, int position) {
    }

    private MyTextWatcher mTextWatcher = new MyTextWatcher();
    private int mCurPosition = -1;

    public void selectedItem(int position) {
        if (mCurPosition != -1) {
            mData.get(mCurPosition).setSelected(false);
            mTextWatcher.clearWatcher();
            notifyItemChanged(mCurPosition);
        }

        mCurPosition = position;
        mData.get(mCurPosition).setSelected(true);
        notifyItemChanged(mCurPosition);
    }

    class MyTextWatcher implements TextWatcher {

        private EditText mEditText;

        void addWatcher(EditText editText) {
            clearWatcher();
            mEditText = editText;
            if (mEditText != null) {
                mEditText.addTextChangedListener(this);
            }
        }

        void clearWatcher() {
            if (mEditText != null) {
                mEditText.removeTextChangedListener(this);
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @SuppressLint("DefaultLocale")
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            clearWatcher();
            WalletCoinRateVoBean item = mData.get(mCurPosition);
            Double money = TextUtils.isEmpty(s.toString()) ? 0 : Double.valueOf(s.toString());
            for (int i = 0; i < mData.size(); i++) {
                if (i == mCurPosition) {
                    mData.get(i).setContent(s.toString());
                    continue;
                }
                double transMoney = transform(money, item.getQuotation(), mData.get(i).getQuotation());
                mData.get(i).setContent(transMoney == 0 ? "" : String.format("%.4f", transMoney));
                notifyItemChanged(i);
            }
            addWatcher(mEditText);
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    }


    private double transform(double money, double fromQuttion, double toQuttion) {
        BigDecimal moneyDecimal = new BigDecimal(money);
        BigDecimal fromDecimal = new BigDecimal(fromQuttion);
        BigDecimal toDecimal = new BigDecimal(toQuttion);
        return moneyDecimal.multiply(toDecimal).divide(fromDecimal, 4, RoundingMode.HALF_UP).doubleValue();
    }


    @Override
    public int getItemViewType(int position) {
        return position < mData.size() ? ITEM_TYPE_NORMAL : ITEM_TYPE_BANNER;
    }

    @Override
    protected int getFooterLayoutCount() {
        return 1;
    }

    static class NormalHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_left)
        ImageView ivLeft;
        @BindView(R.id.tv_title)
        TextView tvTitle;
        @BindView(R.id.et_input)
        EditText etInput;
        @BindView(R.id.tv_currency)
        TextView tvCurrency;
        @BindView(R.id.rl_container)
        CurrencyConverterRelativeLayout rlContainer;

        NormalHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            if (etInput.isFocusable()) {
                etInput.setFocusable(false);
                etInput.setFocusable(true);
            }
            etInput.clearFocus();
        }
    }

    static class BannerHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_banner)
        ImageView ivBanner;

        public BannerHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

}
